package com.wang.music.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wang.music.config.MinioPropertites;
import com.wang.music.config.MinioTemplate;
import com.wang.music.constant.HttpStatusEnum;
import com.wang.music.dao.SingerMapper;
import com.wang.music.entity.Singer;
import com.wang.music.entity.Song;
import com.wang.music.service.SingerService;
import com.wang.music.service.SongService;
import com.wang.music.util.DateUtil;
import com.wang.music.util.FileUtils;
import com.wang.music.util.PageUtils;
import com.wang.music.util.ResMap;
import com.wang.music.vo.PageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 歌手表 服务实现类
 * </p>
 *
 * @author zifan
 * @since 2021-03-05
 */
@Service
public class SingerServiceImpl extends ServiceImpl<SingerMapper, Singer> implements SingerService {
    @Autowired
    private SongService songService;

    @Autowired
    private MinioPropertites minioPropertites;

    @Override
    public Map<String, Object> saveSinger(Singer singer) {
        Map<String, Object> ok = ResMap.OK();
        //查找歌手是否重复
        QueryWrapper<Singer> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Singer::getName, singer.getName());
        Singer getSingerByName = this.getOne(queryWrapper);
        if (ObjectUtil.isNotNull(getSingerByName)) {
            //该歌手已被添加
            ok.put("status", HttpStatusEnum.FAILURE.getStatus());
            ok.put("msg", "该歌手已存在，无法添加。");
            return ok;
        }

        System.out.println(singer.getPic());
        //处理头像
        if (ObjectUtil.isNotNull(singer.getPic())) {
            String s = StrUtil.subAfter(singer.getPic(), "8888", true);
            System.out.println(s);
            singer.setPic(s);
        }

        this.save(singer);
        ok.put("msg", "添加成功");
        return ok;
    }

    @Override
    public Map<String, Object> getAllSinger(Page<Singer> page) {
        Map<String, Object> resMap = ResMap.OK();
        this.page(page);
        PageVO pageVO = PageUtils.toPageVO(page);
        resMap.put("page", pageVO);
        return resMap;
    }

    @Override
    public Map<String, Object> getSingerByName(String name) {
        Map<String, Object> resMap = ResMap.OK();
        QueryWrapper<Singer> queryWrapper = new QueryWrapper<>();
        if (ObjectUtil.isNotNull(name)) {
            queryWrapper.lambda().like(Singer::getName, name);
        }
        List<Singer> list = this.list(queryWrapper);
        resMap.put("list", list);
        return resMap;
    }

    @Override
    public Map<String, Object> updateSinger(Singer singer) {
        Map<String, Object> resMap = ResMap.OK();
        this.updateById(singer);
        resMap.put("msg", "更新成功");
        return resMap;
    }

    @Override
    public Map<String, Object> delSinger(Singer singer) {
        //todo 删除歌手的时候不能直接删除，得判断下该名歌手有没有对应的歌曲，有的话提示不能删除（3/9 18点06分已做）
        Map<String, Object> resMap = ResMap.OK();

        QueryWrapper<Song> wrapper = new QueryWrapper<>();
        Integer singerId = singer.getId();
        wrapper.lambda().eq(Song::getSingerId, singerId);
        List<Song> songs = songService.list(wrapper);
        if (!(songs == null || songs.size() == 0)) {
            resMap.put("msg", "无法删除，改名歌手下有歌曲！");
            return resMap;
        }
        this.removeById(singerId);
        resMap.put("msg", "删除成功");
        return resMap;
    }

    @Override
    public Map<String, Object> changePic(MultipartFile pic) {
        Map<String, Object> ok = ResMap.OK();
        MinioTemplate minioTemplate = new MinioTemplate(FileUtils.getMinioClient(minioPropertites));
        try {
            //对文件命做处理
            String filename = DateUtil.FormatDateForUpload(pic.getOriginalFilename());
            String bucket = minioPropertites.getBucket();
            minioTemplate.putObject(bucket,filename,pic.getInputStream());
            //返回的url时页面上显示的url地址
            ok.put("url",minioPropertites.getUrl() + "/" + bucket + filename);
            ok.put("name",filename);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ok;
    }

    @Override
    public List<Singer> allSinger() {
        return this.list();
    }
}
